<template>
  <div class="reg-container">
    <div class="box-reg">
      <div class="box-reg-title"></div>
      <!-- 登录的表单 -->
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormref">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请填写用户名"></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login">登录</el-button>
          <el-link type="info" @click="$router.push('/reg')">去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user.js'
export default {
  name: 'Reg',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '这是必填项', trigger: 'blur' },
          { pattern: /^[0-9a-zA-Z]{1,10}$/, message: '用户名为1-10的字符', trigger: 'change' }
        ],

        password: [
          { required: true, message: '密码为必填项', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码为6-15位的非空字符串', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormref.validate(async valid => {
        if (!valid) return
        // const { data: res } = await axios.post('http://www.liulongbin.top:3008/api/login', this.loginForm)
        const { data: res } = await loginAPI(this.loginForm)
        if (res.code !== 0) return this.$message.error('账号或密码不一致')
        this.$message.success(res.message)
        this.$store.commit('user/updateToken', res.token)
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  height: 100%;
  background: url('../assets/images/login_bg.jpg') no-repeat;
  background-size: cover;
  .box-reg {
    padding: 0 20px;
    width: 400px;
    height: 270px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    .box-reg-title {
      height: 60px;
      background: url('../assets/images/login_title.png') no-repeat;
      background-position: center;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>
